package com.ambercrm.tools;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.content.FileProvider;

import java.util.List;

public class ApkTools {
	static Context mContext;
	static ApkTools mInstance;

	private ApkTools(Context context) {
		mContext = context;
	}

	public static ApkTools getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new ApkTools(context);
		}
		return mInstance;
	}

	public boolean isInstallOnSDCard(String packageName) {
		try {
			PackageManager pm = mContext.getPackageManager();
			ApplicationInfo appInfo;
			appInfo = pm.getApplicationInfo(packageName, 0);

			if ((appInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// 判断微信是否安装
	public static boolean isWeixinInstall(Context context) {
		final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				String pn = pinfo.get(i).packageName;
				if (pn.equals("com.tencent.mm")) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * 判断qq是否可用
	 *
	 * @param context
	 * @return
	 */
	public static boolean isQQInstall(Context context) {
		final PackageManager packageManager = context.getPackageManager();
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				String pn = pinfo.get(i).packageName;
				if (pn.equals("com.tencent.mobileqq")) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 启动微信
	 * @param context
	 */
	public static void launchWechat(Context context) {
		Intent intent = new Intent();
		ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
		intent.setAction(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setComponent(componentName);
		context.startActivity(intent);
	}

	/**
	 * 是应用已经安装
	 * @param context
	 * @return
	 */
	public static boolean isAppInstall(Context context, String packagename) {
		final PackageManager packageManager = context.getPackageManager();
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				String pn = pinfo.get(i).packageName;
				if (pn.equals(packagename)) {
					return true;
				}
			}
		}
		return false;
	}

	// 8.0以上系统调用安装应用
	public static void startInstallApk(String apkName, Activity context) {
		if (Build.VERSION.SDK_INT >= 26) {
			boolean hasInstallPermission = context.getPackageManager().canRequestPackageInstalls();
			if (!hasInstallPermission) {
				//注意这个是8.0新API
				Uri packageURI = Uri.parse("package:" + context.getPackageName());
				Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
				context.startActivityForResult(intent, 10025);
			}
			else {
				installApk(apkName, context);
			}
		}
		else {
			installApk(apkName, context);
		}
	}

	// 8.0以下系统安装应用
	private static void installApk(String apkName, Activity context) {
		FileUtils mFileUtils = new FileUtils();
		Uri mUri;
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(Intent.ACTION_VIEW);
		//7.0系统开始认为直接使用本地Uri是不安全的 所以以FileProvider共享给外部提高安全性
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			mUri = FileProvider.getUriForFile(context, "com.djframe.fileProvider", mFileUtils.getApksFile(apkName));
		} else {
			mUri = Uri.fromFile(mFileUtils.getApksFile(apkName));
		}
		//添加这一句表示对目标应用临时授权该Uri所代表的文件
		intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		intent.setDataAndType(mUri,
				"application/vnd.android.package-archive");
		context.startActivityForResult(intent, 10025);
	}
}
